package com.yunban.project.template.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunban.project.template.mapper.TypeMapper;
import com.yunban.project.template.model.domain.Type;
import com.yunban.project.template.model.dto.TypeReq;
import com.yunban.project.template.model.dto.TypeSearchReq;
import com.yunban.project.template.model.vo.TypeAttributeResp;
import com.yunban.project.template.model.vo.TypeResp;
import com.yunban.project.template.service.TypeAttributeService;
import com.yunban.project.template.service.TypeService;
import jakarta.annotation.Resource;
import net.lab1024.sa.base.common.domain.PageResult;
import net.lab1024.sa.base.common.exception.BusinessException;
import net.lab1024.sa.base.common.util.SmartPageUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author: lijiajun
 * @date: 2024-11-06
 * @version: 1.0
 */
@Service
public class TypeServiceImpl extends ServiceImpl<TypeMapper, Type> implements TypeService {

    @Resource
    private TypeAttributeService typeAttributeService;

    @Override
    public Type findByCodeOrName(String code, String name) {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Type::getName, name).or(w -> w.eq(Type::getCode, code));
        return this.getOne(wrapper);
    }

    @Override
    public Type findById(long id) {
        Type type = this.getById(id);
        if (ObjectUtils.isEmpty(type)) {
            throw new BusinessException("此类型不存在");
        }
        return type;
    }

    @Override
    public PageResult<TypeResp> getTypeList(TypeSearchReq req) {
        LambdaQueryWrapper<Type> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.hasText(req.getName())) {
            wrapper.like(Type::getName, req.getName());
        }
        if (StringUtils.hasText(req.getCode())) {
            wrapper.eq(Type::getCode, req.getCode());
        }
        Page<Type> page = this.page(SmartPageUtil.convert2PageQuery(req), wrapper);
        PageResult<TypeResp> result = SmartPageUtil.convert2PageResult(page, TypeResp.class);
        // 获取所有类型属性信息
        List<TypeAttributeResp> attributeList = this.typeAttributeService.getAllTypeAttribute();
        result.getList().forEach(items -> {
            attributeList.forEach(attribute -> {
                if (items.getId().equals(attribute.getBlockTypeId())) {
                    items.getAttributeList().add(attribute);
                }
            });
        });
        return result;
    }

    @Override
    public Long saveType(TypeReq req) {
        if (!ObjectUtils.isEmpty(this.findByCodeOrName(req.getCode(), req.getName()))) {
            throw new BusinessException("此类型已存在");
        }
        Type type = new Type();
        BeanUtils.copyProperties(req, type);
        this.save(type);
        return type.getId();
    }

    @Override
    public Boolean editType(long id, TypeReq req) {
        Type t = this.findByCodeOrName(req.getCode(), req.getName());
        if (!ObjectUtils.isEmpty(t) && t.getId() != id) {
            throw new BusinessException("此类型已存在");
        }
        t = this.findById(id);
        BeanUtils.copyProperties(req, t);
        return this.updateById(t);
    }

    @Override
    public Boolean delType(long id) {
        Type type = this.findById(id);
        return this.removeById(type);
    }
}
